< previous page page_111 next page >

Page 111
What Is a Module?
A module is a single file that contains one or more routines (procedures or functions) and optionally a user interface (a form). A variable thats declared at the top of one of these modules (outside of any routine) is considered a module-level variable. This variable can only be viewed and changed by any routine within this file. This variable will stay in memory, or have a lifetime, as long as that module is in memory.
There are three different types of modules: standard modules (.BAS extension), form modules (.FRM extension), and class modules (.CLS extension). A standard module is a global repository for routines and public variables. A form module is a Visual Basic form that has a user interface and optionally some public and private variables. A class module is a template from which you can create your own objects. A class module can have both private and public variables. Youll learn to use all three throughout this book.
Limiting Your Scope
You should strive to limit the scope as much as possible. Public variables are hard to keep track of, and you never know when some other routine may use that same variable for its own purposes and wipe out the value you were expecting. This can cause a lot of confusion in your programs and may result in your variables values changing for no apparent reason. Module-level variables are much better, because only the routines in one file may use them. Local variables are the best, because only one routine can view and change them. See Figure 6.1 for a graphical representation of scoping rules. This makes your data very isolated with less chance for corruption.
8160-0111a.gif
Figure 6.1
Scoping rules.

 
< previous page page_111 next page >

If you like this book, buy it!